<template>
	<view class="distribution-posters">
		<view class="slider-banner banner">
			<swiper indicatorDots="true">
				<block v-for="(item, infoIndex) in info" :key="infoIndex">
					<swiper-item>
						<image class="slide-image" :src="item.wap_poster" mode="widthFix" show-menu-by-longpress
							@tap="previewImg(item.wap_poster)" />
					</swiper-item>
				</block>
			</swiper>
		</view>
	</view>
</template>
<script>
	import {
		getWarrantImg
	} from "@/api/user";
	import cookie from '@/utils/store/cookie'
	export default {
		name: "Warrant",
		components: {},
		data: function() {
			return {
				info: [],
				activeIndex: 0
			};
		},
		onShow: function() {
			this.$store.dispatch('getUser').then(res => {
				console.log("获取用户信息");
				console.log(res)
				if(res.bankUserName == null){
					uni.showModal({
						title: '温馨提示',
						content: '您的账户还未实名认证，请前往个人中心补充资料',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/user/PersonalData/index'
								});
							} else if (res.cancel) {
								uni.navigateBack(1)
							}
						}
					});
				}else if(res.cardId == ''){
					uni.showModal({
						title: '温馨提示',
						content: '您的账户还未实名认证，请前往个人中心补充资料',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/user/PersonalData/index'
								});
							} else if (res.cancel) {
								uni.navigateBack(1)
							}
						}
					});
				}else if(res.phone == null){
					uni.showModal({
						title: '温馨提示',
						content: '您的账户还未绑定手机号，请前往个人中心绑定再操作',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/user/PersonalData/index'
								});
							} else if (res.cancel) {
								uni.navigateBack(1)
							}
						}
					});
				}else this.getIndex();
				
			}).catch( err => {
			    console.log(err);
			})
			let that = this;
		},
		methods: {
			previewImg(imagesUrl) {
				let _this = this;
				let imgsArray = [];
				imgsArray[0] = imagesUrl
				uni.previewImage({
					current: 0,
					urls: imgsArray
				});
			},
			getIndex: function() {
				let that = this;
				uni.showLoading({
					title: "加载中"
				});
				
				getWarrantImg({
					from: this.$deviceType == 'weixin' || this.$deviceType == 'weixinh5' ? 'uniappH5' : this
						.$deviceType
				}).then(
					res => {
						uni.hideLoading();
						that.info = res.data;
						
						console.log(that.info)
					},
					err => {
						uni.hideLoading();
						uni.showToast({
							title: err.msg || err.response.data.msg || err.response.data.message,
							icon: "none",
							duration: 2000
						});
					}
				);
			},
			downloadIamge(imgsrc, name) {
				var that = this;
				this.isDown = true;
				var downloadUrl = imgsrc;

				if (!wx.saveImageToPhotosAlbum) {
					uni.showModal({
						title: "提示",
						content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
					});
					that.openDialogVisible = true;

					return;
				}
			},
			saveImg: function() {
				this.downloadIamge(
					this.info[this.activeIndex].wap_poster,
					"poster" + this.activeIndex
				);
			},
			downloadFile(url) {
				uni.downloadFile({
					url,
					fail: function(res) {
						uni.showModal({
							title: "提示",
							content: "保存失败"
						});
					},
					success: function(res) {
						uni.showModal({
							title: "提示",
							content: "保存成功"
						});
					}
				});
			}
		}
	};
</script>

<style lang="less">
	page {
		height: 100%;
	}

	.distribution-posters {
		height: 100%;
	}

	.banenr {
		height: 100%;
	}

	.banner swiper {
		height: 100%;
	}

	.banner .slide-image {
		width: 100%;
		height: auto;
	}
</style>
